// Main modules
site.module('toolbar', function(context) {
	var toggle_box = context.require('toggleBox'),
		has = 'hasOwnProperty',
		

   //helper function to hide boxes  
   		hide_boxes = function(anchor){
   			var temp_anchor,
   				boxes = [{'anchor': '#search_icon',
                  		  'box': '#search_box'},
                 
		                 {'anchor': '#contact_icon',
		                  'box': '#invitation_box'},

		                 {'anchor': '#message_icon',
		                  'box': '#inbox_box'},

		                  {'anchor': '#notification_icon',
		                   'box': '#notification_box'}];

   			for(var i in boxes) {

   				if(boxes[has](i)) {
   					var a = boxes[i].anchor;

   					if(a.indexOf(anchor) !== -1) continue;

	   				temp_anchor = context.query(a);
	   				if(temp_anchor.hasClass('active')) {
	   					temp_anchor.removeClass('active');
	   					context.query(boxes[i].box).hide();
	   				}
   				}	
   			}  
    	},
    
		search_toggle_box = function() {
			
			toggle_box.init({ 
				anchor: '#search_icon', 
				box: '#search_box', 
				beforeToggle: function() {
					hide_boxes();	
				},
				onToggle: function() {
					//cleare old error style input and reset input values
					context.query('#search_box form')[0].reset();
				}
			});
		},

		contact_toggle_box = function() {
			var query = $ = context.query,
				request	= context.request,
				require = context.require,
				notify = require('notify'),
				processing = false;

			toggle_box.init({
				anchor: '#contact_icon',
				box: '#invitation_box',
				beforeToggle: function() {
					hide_boxes(this.attr('id'));	
				},
				onToggle: function() {
					if(query('#invitation_box .toggle_loader').length){
						request({
							url: '/contact/get_toggle',
		                    type: 'post',
		                    dataType: 'json',
		                    success: function(data)
		                    {
		                    	query('#invitation_box .list').html(data.content);
		                    	query('#contact_icon > .toolbar_notice').fadeOut();

		                    	var buttons = query('.pending_request');

								buttons.on('click', function(e){
									if(!processing){
										processing = true;
										var anchor = $(this);
										request({
											url: anchor.attr('href'),
											type: 'get',
											dataType: 'json',
											success: function(data) {

												if(data.success == true){
													notify.create('create', {
																title: data.message,
																text:  data.description
															});
												anchor.parents('li').slideUp();

												}
											},
											complete: function() {
												processing = false;
											}

										});
									}					
									e.preventDefault();
								}) 
		                    }
						})
					}
				}
			});
		},

		message_toggle_box = function() {

			toggle_box.init({
				anchor: '#message_icon', 
				box: '#inbox_box',
				beforeToggle: function() {
					hide_boxes(this.attr('id'));	
				}, 
				onToggle: function() {
					if(context.query('#inbox_box .toggle_loader').length){
						context.request({
							url: '/message/get_toggle',
		                    type: 'post',
		                    dataType: 'json',
		                    success: function(data)
		                    {
		                    	context.query('#inbox_box .list').html(data.content);
		                    	context.query('#message_icon > .toolbar_notice').fadeOut();
		                    }
						})
					}
				}
			});
		},


		notification_toggle_box = function() {

			toggle_box.init({
				anchor: '#notification_icon',
				box: '#notification_box',
				beforeToggle: function() {
					hide_boxes(this.attr('id'));	
				},
				onToggle: function() {
					if(context.query('#notification_box .toggle_loader').length){
						context.request({
							url: '/notification/get_toggle',
		                    type: 'post',
		                    dataType: 'json',
		                    success: function(data)
		                    {
		                    	context.query('#notification_box .list').html(data.content);
		                    	context.query('#notification_icon > .toolbar_notice').fadeOut();
		                    }
						})
					}
			}});
		};


	return {
		init: function(settings) {
			search_toggle_box();
			contact_toggle_box();
			message_toggle_box();
			notification_toggle_box();
		},

		events: {
			'window.click': function() { 
				hide_boxes();
			}
		}
	}
})

.module('window.events', function(context) {
	return {
		init: function() {
			context.query('html').on('click', function(e) {
				context.trigger('window.click', e);
			});
		}
	}
});